package com.chen.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.blog.common.api.CommonResult;
import com.chen.blog.common.component.ImgUtil;
import com.chen.blog.common.constant.MessageConstant;
import com.chen.blog.model.Blog;
import com.chen.blog.service.BlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chen
 * @since 2022-01-24
 */
@Api(tags = "博客管理 BlogController")
@RestController
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private ImgUtil imgUtil;

    private static String blogImgPath;

    @ApiOperation("发表博客")
    @PostMapping
    public CommonResult addBlog(@RequestBody Blog blog) {
        blog.setImg(blogImgPath);
        return blogService.addBlog(blog);
    }

    @ApiOperation("上传博客封面")
    @PostMapping("/pic")
    public CommonResult uploadPic(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return CommonResult.error(MessageConstant.UPLOAD_PIC_FAIL);
        }
        String fileName = System.currentTimeMillis() + file.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "img";
        File realFile = new File(filePath);
        if (!realFile.exists()) {
            realFile.mkdir();
        }
        File realPath = new File(filePath + System.getProperty("file.separator") + fileName);
        try {
            file.transferTo(realPath);
            blogImgPath = "/img/" + fileName;
        } catch (IOException e) {
            e.printStackTrace();
            return CommonResult.success(MessageConstant.UPLOAD_PIC_FAIL);
        }
        return CommonResult.success(MessageConstant.UPLOAD_PIC_SUCCESS);
    }

    @ApiOperation("删除博客")
    @DeleteMapping("/delete/{id}")
    public CommonResult deleteBlog(@PathVariable Integer id) {
        return blogService.deleteBlog(id);
    }

    @ApiOperation("更新博客封面")
    @PostMapping("/pic/{id}")
    public CommonResult updatePic(@RequestParam("file") MultipartFile file, @PathVariable Integer id) {
        return imgUtil.updatePic(file, id, blogService.getById(id));
    }

    @ApiOperation("获取指定id博客")
    @GetMapping("/detail/{id}")
    public List<Map<String, Object>> getById(@PathVariable Integer id) {
        return blogService.getByBlogId(id);
    }

    @ApiOperation("获取指定用户发布的博客")
    @GetMapping("/release/{id}")
    public CommonResult getBlogByUid(@PathVariable Integer id) {
        return blogService.getBlogByUid(id);
    }

    @ApiOperation("检索博客")
    @GetMapping("/list/{currentPage}/{pageSize}")
    public CommonResult getAllBlog(@PathVariable(value = "currentPage") Integer currentPage, @PathVariable(value = "pageSize") Integer pageSize, @RequestParam Map<String, Object> map) {
        return blogService.getAllBlog(currentPage, pageSize, map);
    }

    @ApiOperation("搜索博客")
    @GetMapping("/search")
    public CommonResult searchAllBlog(@RequestParam(defaultValue = "") String keywords) {
        return blogService.searchAllBlog(keywords);
    }

    @ApiOperation("最新博客")
    @GetMapping("/new")
    public List<Blog> getNewBlog() {
        return blogService.list(new QueryWrapper<Blog>().select("id", "title").orderBy(true, false, "create_time").last("limit 0,3"));
    }

    @ApiOperation("热门博客")
    @GetMapping("/hot")
    public List<Blog> getHotBlog() {
        return blogService.list(new QueryWrapper<Blog>().select("id", "title").orderBy(true, false, "views").last("limit 0,3"));
    }
}
